package com.keelau.ui.fx;

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.ContextMenuHandler;
import com.teamdev.jxbrowser.chromium.ContextMenuParams;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;


public class BrowserContextMenu implements ContextMenuHandler {

    private final StackPane parent;

    AliDetailCrawlerUI crawlerUI;

    public BrowserContextMenu(StackPane parentComponent, AliDetailCrawlerUI crawlerUI) {
        this.parent = parentComponent;
        this.crawlerUI = crawlerUI;
    }



    public void showContextMenu(final ContextMenuParams params) {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                createAndDisplayContextMenu(params);
            }
        });
    }

    public static void setSysClipboardText(String writeMe) {
        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable tText = new StringSelection(writeMe);
        clip.setContents(tText, null);
    }

    private void createAndDisplayContextMenu(final ContextMenuParams params) {
        final ContextMenu contextMenu = new ContextMenu();

        // Since context menu doesn't auto hide, listen mouse press events
        // on BrowserView and hide context menu on mouse press
        parent.getChildren().get(0).setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                contextMenu.hide();
            }
        });

        // If there's link under mouse pointer, create and add
        // the "Open link in new window" menu item to our context menu
        if (!params.getLinkText().isEmpty()) {
            contextMenu.getItems().add(createMenuItem(
                    "新窗口打开", new Runnable() {
                        public void run() {
                            String linkURL = params.getLinkURL();
                            crawlerUI.createBrowserTab(linkURL, null);
                        }
                    }));
        }

        // Create and add "Reload" menu item to our context menu
        contextMenu.getItems().add(createMenuItem("重新加载", new Runnable() {
            public void run() {
                params.getBrowser().reload();
            }
        }));

        contextMenu.getItems().add(createMenuItem("返回", new Runnable() {
            @Override
            public void run() {
                Browser browser = params.getBrowser();
                if(browser.canGoBack())
                  browser.goBack();
            }
        }));

        contextMenu.getItems().add(createMenuItem("前进", new Runnable() {
            @Override
            public void run() {
                if(params.getBrowser().canGoForward()){
                    params.getBrowser().goForward();
                }
            }
        }));

        contextMenu.getItems().add(createMenuItem("复制源代码", new Runnable() {
            @Override
            public void run() {
                setSysClipboardText( params.getBrowser().getHTML());
            }
        }));

        // Display context menu at required location on screen
        Point location = params.getLocation();
        Point2D screenLocation = parent.localToScreen(location.x, location.y);
        contextMenu.show(parent, screenLocation.getX(), screenLocation.getY());
    }
    private static MenuItem createMenuItem(String title, final Runnable action) {
        MenuItem menuItem = new MenuItem(title);

        menuItem.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                action.run();
            }
        });
        return menuItem;
    }
}
